% ************************************************************************ 
%     Module name   : Fusion Simulation - main framework function                                                   
%                                                                          
%     Description   : This will house the simulation.                                                    
%                                                                          
%     $Header: /Lynx/SimulationFramework/RSP/IFS/IFSRun.m 2     15/10/08 16:08 Lwabeke $ 
%                                                                          
%    $Author: Lwabeke $	            
%                                                                         
%    $Revision: 2 $                
%                                                                         
% ************************************************************************
%     $History: IFSRun.m $ 
% 
%
% ************************************************************************

if ~exist('schedPerf', 'var')
    global schedPerf;

    schedPerf.iterations = 0;
end

%%
tic

time = 0;

% Initialise the parameters
if ~exist('param','var')
    param = Param_Init(time);
end

% Initialise the world
global worldState;
[worldState] = World_Init(time, param);

% s = RandStream.create('mt19937ar','seed',now);
%             RandStream.setDefaultStream(s);

% Initialise the radar
[radarState] = Radar_Init(time, param);

[scheduleState] = scheduleTargets_Init(time, param);


currentTime = time;

global perfHist;
perfHist = Performance_Init(param);
visualiseState = [];

%[perfHist.search.hist] = Search_Init(time, param);

while (currentTime< param.endTime)
    
    
    [currentTime]  = World_Run(currentTime, param);
    
%     radarState.TargetList.Targets = [];           % Used to check only search mode
%     radarState.TargetList.numTargets = 0;
%    

         [TargetSequence, TargetAngles, TargetSequenceBinned] = predictIntercepts(currentTime, radarState, param);

         [ActiveTargetSequence, scheduleState] = scheduleTargets_Run(currentTime, scheduleState, TargetSequenceBinned, param);
         
         if (length(ActiveTargetSequence) < 1)
           TargetSequence  
           ActiveTargetSequence
           dummy = 1;
         end
         
         [radarSetup] = parseSchedule(currentTime, radarState, TargetSequence, TargetAngles, ActiveTargetSequence, param);
%         [radarSetup] = setup_select(currentTime, radarState, tgtCntr, param);


%    for tgtCntr=0:radarState.TargetList.numTargets
    for tgtCntr=1:radarSetup.numSegments
       
%         % tgtCntr = 0 is for non-target specific waveforms, i.e.
%         % search-mode
%         
%         [radarSetup] = setup_select(currentTime, radarState, tgtCntr, param);
        
        [radarState, new_time, changedInfo] = Radar_Run(currentTime, radarState, tgtCntr, radarSetup, param);

%         [reward] = RewardCalc(changedInfo, radarState);
% %        [reward] = RewardCalc_cheat(changedInfo, radarState, worldState);
        
%         [radarState] = radar_learn(changedInfo, radarState, reward, param);

%         if (tgtCntr == 0)
%             searchSetup = radarSetup;
%             searchSetup.reward = reward;
%             searchSetup.changedInfo = changedInfo;
%         end
        
        % Step world time
        [currentTime]  = World_Run(new_time, param);

    end
    
    
%     % Drop targets that have been lost/classified as false alarms
%     [radarState] = radar_discard(radarState, param);

    %% Simulation measurements
%    radarState.searchSetup = searchSetup;
    %[perfHist] = measurePerformance(currentTime, radarState, worldState, param, perfHist);
%    measurePerformance(currentTime, radarState, worldState, param);
    [visualiseState] = visualise(currentTime, radarState, worldState, visualiseState, param);
    
%     radarState.radar(1).posAngle
%     radarState.radar(2).posAngle
end

figure(2)
hold off;

%% 
  display_performance(perfHist, visualiseState, param);


perfHist.elapsed_time = toc; toc
